﻿using ShoppingTemplate.Data;
using ShoppingTemplate.DataModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace ShoppingTemplate
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class CategoryPage : ShoppingTemplate.Common.LayoutAwarePage
    {
        private DataTransferManager dataTransferManager;
        public CategoryPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]
            string param = navigationParameter as string;
            var groups = new SampleDataSource1().ItemGroups;
            var group = groups.Where(p => p.UniqueId == param).FirstOrDefault();
            //pageTitle.Text = (group as SampleDataGroup1).Title;

            PopulateCombobox();
            switch ((group as SampleDataGroup1).Title)
            {
                case "Category 1":
                    cmbCategory.SelectedIndex = 0;
                    break;
                case "Category 2":
                    cmbCategory.SelectedIndex = 1;
                    break;
                case "Category 3":
                    cmbCategory.SelectedIndex = 2;
                    break;
            }

            this.DefaultViewModel["Items"] = (group as SampleDataGroup1).Items;

            Window.Current.SizeChanged += Current_SizeChanged;
            this.dataTransferManager = DataTransferManager.GetForCurrentView();
            this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
        }

        private void PopulateCombobox()
        {
            List<FlyoutData> lstFD = new List<FlyoutData>();
            lstFD.Add(new FlyoutData() { Name = "Category 1" });
            lstFD.Add(new FlyoutData() { Name = "Category 2" });
            lstFD.Add(new FlyoutData() { Name = "Category 3" });

            cmbCategory.ItemsSource = lstFD;
        }

        void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var request = args.Request;
            string dataPackageText = "Open a product detail you'd like to share and try again.";
            request.FailWithDisplayText(dataPackageText);
        }

        void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            if (ApplicationView.Value == ApplicationViewState.Snapped)
            {
                this.TopAppBar = null;
            }
            else
            {
                this.TopAppBar = this.topappBar;
            }
        }
        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            this.dataTransferManager.DataRequested -= new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
        }

        private void itemGridClicked(object sender, ItemClickEventArgs e)
        {
            var item = e.ClickedItem as SampleDataItem1;
            this.Frame.Navigate(typeof(GroupedItemsPage), item.Title);
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.GoHome(sender, e);
        }

        private void imgLogo_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            this.GoHome(sender, e);
        }

        private void cmbCategoryFiltered(object sender, SelectionChangedEventArgs e)
        {
            var selectedTitle = (cmbCategory.SelectedItem as FlyoutData).Name;
            var groups = new SampleDataSource1().ItemGroups;
            var group = groups.Where(p => p.Title == selectedTitle).FirstOrDefault();
            this.DefaultViewModel["Items"] = group.Items;
        }
    }

    public class FlyoutData
    {
        public string Name { get; set; }
        public string Id { get; set; }
    }
}
